﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using QLKDS.DAL;
using QLKDS.Helper;

namespace QLKDS.Forms
{
    public partial class frmUserAdd : XtraForm
    {
        #region Properties
        
        private UserManager userManager;
        private EmployeeManager employeeManager = new EmployeeManager();

        #endregion

        #region Methods

        private void SetMaxLengthOfTextBoxes()
        {
           // this.txtName.Properties.MaxLength = 32;            
            this.txtMobile.Properties.MaxLength = 16;           
        }

        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            
         //   this.txtName.Text = String.Empty;
            this.txtMobile.Text = String.Empty;
            this.txtAddress.Text = String.Empty;
            this.txtEmail.Text = String.Empty;
            this.txtDescription.Text = String.Empty;
            //this.txtName.Focus();
        }
        private void LoadEmployee()
        {
            this.txtName.Properties.DataSource = this.employeeManager.ListEmployee();
            this.txtName.Properties.ValueMember = "Id";
            this.txtName.Properties.DisplayMember = "Fullname";
        }
        private void txtName_EditValueChanged_1(object sender, EventArgs e)
        {
            this.txtMobile.Text = this.employeeManager.GetEmployeeById((int)txtName.EditValue).Mobile;
            this.txtAddress.Text = this.employeeManager.GetEmployeeById((int)txtName.EditValue).Address;
            this.txtDescription.Text = this.employeeManager.GetEmployeeById((int)txtName.EditValue).Description;
            this.txtEmail.Text = this.employeeManager.GetEmployeeById((int)txtName.EditValue).Email ;
        }
        #endregion

        #region Init form
        public frmUserAdd()
        {
            InitializeComponent();
            this.userManager = new UserManager();
       
        }

        private void frmStoreAdd_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
            this.SetMaxLengthOfTextBoxes();
            this.LoadEmployee();
           
        }
        #endregion

        #region Validate

        private void txtName_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtName.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepName.ClearErrors();
            }
        }

       

       

        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            var user = new User();


            //validation name
            if (this.txtName.Text.Trim().Length == 0)
            {
                this.dxepName.SetError(this.txtName, "Tên nhân viên không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtName.Focus();
                return;
            }
            else
            {
                this.dxepName.ClearErrors();
            }
            //end validation name

            //validation username
            if (this.txtUsername.Text.Trim().Length == 0)
            {
                this.dxepUsername.SetError(this.txtUsername, "Tên đăng nhập không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtUsername.Focus();
                return;
            }
            else if(this.userManager.HasUsernameExisted(txtUsername.Text.Trim()))
            {
                this.dxepUsername.SetError(this.txtUsername, "Tên đăng nhập đã tồn tại trong hệ thống.Chọn tên đăng nhập khác", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtUsername.Focus();
                return;
             }
            else
            {
                this.dxepUsername.ClearErrors();
            }

            //validation password
            if (this.txtPassword.Text.Trim().Length == 0)
            {
                this.dxepPassword.SetError(this.txtPassword, "Mật khẩu không được để trống.", DevExpress.XtraEditors.DXErrorProvider.ErrorType.User1);
                this.txtPassword.Focus();
                return;
            }
            else
            {
                this.dxepPassword.ClearErrors();
            }


            // End of validation
            
            /*****************************************************************/
            // Save into database                   

            user.Status = 1;
           
            user.Username = this.txtUsername.Text.Trim();
            user.Password = EncryptionPassword.GetMD5(this.txtPassword.Text.Trim());
            user.Status = 1;
            user.EmployeeId = (int)txtName.EditValue;
          
            this.userManager.CreateUser(user);

        XtraMessageBox.Show("Dữ liệu thêm thành công.");
        this.DialogResult = DialogResult.OK;
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = DialogResult.Cancel;
        }

        #endregion

     

      

 

      

     
        


    }
}
